/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.oimwrapper.testers;

import com.thortech.util.logging.Logger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.oimwrapper.connectors.OIMAdapterBase;
import org.oimwrapper.connectors.OIMFormData;
import org.oimwrapper.connectors.OIMRemoteClientBase;
import org.oimwrapper.connectors.OIMRequest;

/**
 *
 */
public class ClientCustomRequestTester extends ClientBaseTester {



    private static final Logger logger = Logger.getLogger(ClientCustomRequestTester.class.getName());

    private static OIMAdapterBase apiBase;
    private OIMRemoteClientBase tester;

    @Test
    public void mainTest()
    {
        logger.debug("Starting ");
        logger.debug("Logging In ");
        try {
            tester = this.connect();
            apiBase = tester.getUtils();
        } catch (Exception ex) {
            logger.error("Exception",ex);
            return;
        }

        try {

            Map requestData = apiBase.getRequest(1L);
            logger.debug("Request 1:" + requestData);
            requestData = apiBase.getRequest(2L);
            logger.debug("Request 2:" + requestData);

        } catch (Exception ex) {
            logger.error("Exception",ex);
            return;
        }


        try
        {
            OIMFormData form = new OIMFormData();
            OIMRequest oimRequest = new OIMRequest(OIMRequest.RequestForOrg);

            String requesterId = "AA11944";
            String targetUserId = "AA10175";
            String externalReqId = "999998";
            
            String requesterKeyStr = apiBase.getOimUserAttribute(requesterId, "Users.Key");

            if (requesterKeyStr == null)
            {
                logger.error("Failed to get User " + requesterId);
                return;
            }

            Map parentData = new HashMap();
            parentData.put("UD_CUSTREQ_RESOURCENAME","ADD_WINDOWS_GROUPS");
            parentData.put("UD_CUSTREQ_REQUESTER",requesterId);
            parentData.put("UD_CUSTREQ_TARGET_USER",targetUserId);
            parentData.put("UD_CUSTREQ_EXTERNAL_REQID",externalReqId);

            form.setParentData(parentData);

            Map childData = new HashMap();
            ArrayList childDataMapList = new ArrayList();

            childData.put("UD_CUSTREQC_ROLENAME", "LOCALUSER");
            childDataMapList.add(childData);
            childData = new HashMap();
            childData.put("UD_CUSTREQC_ROLENAME", "VENDOR");
            childDataMapList.add(childData);
            form.setChildData("UD_CUSTREQC", (Map[])childDataMapList.toArray(new Map[0]));

            oimRequest.addResourceObject(apiBase.getResourceObject("Custom Resource Request"), form);
            
            //long requesterKey = Long.parseLong(requesterKeyStr);
            long orgKey = apiBase.getOrganization("Requests");
            oimRequest.addTargetOrg(orgKey);
            long reqKey = apiBase.createRequest(oimRequest, orgKey);
            Map requestData = apiBase.getRequest(reqKey);
            logger.debug("Request Created " + reqKey + ":" + orgKey);

        }
        catch(Exception ex)
        {
            logger.error("Exception",ex);
        }
        tester.disconnect();
        //System.exit(0);


    }
}
